package com.grace.portal.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.grace.portal.entity.WxMenu;

public class ClientTest {

	public static void main(String[] args) {
		
		List<WxMenu> list = new ArrayList<WxMenu>();
		WxMenu wxMenuTop01 = new WxMenu();
		wxMenuTop01.setId(1L);
		wxMenuTop01.setType("click");
		wxMenuTop01.setName("素质评估");
		wxMenuTop01.setKey("MENU_LEVEL_01");
		
		List<WxMenu> subList1 = new ArrayList<WxMenu>();
		List<WxMenu> subList2 = new ArrayList<WxMenu>();
		List<WxMenu> subList3 = new ArrayList<WxMenu>();
		
		WxMenu wxMenuTop0101 = new WxMenu();
		wxMenuTop0101.setId(2L);
		wxMenuTop0101.setType("view");
		wxMenuTop0101.setName("成绩分析");
		wxMenuTop0101.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList1.add(wxMenuTop0101);
		
		WxMenu wxMenuTop0102 = new WxMenu();
		wxMenuTop0102.setId(3L);
		wxMenuTop0102.setType("view");
		wxMenuTop0102.setName("考勤分析");
		wxMenuTop0102.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList1.add(wxMenuTop0102);
		
		WxMenu wxMenuTop0103 = new WxMenu();
		wxMenuTop0103.setId(3L);
		wxMenuTop0103.setType("view");
		wxMenuTop0103.setName("奖罚分析");
		wxMenuTop0103.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList1.add(wxMenuTop0103);
		
		WxMenu wxMenuTop0104 = new WxMenu();
		wxMenuTop0104.setId(3L);
		wxMenuTop0104.setType("view");
		wxMenuTop0104.setName("综合评估");
		wxMenuTop0104.setUrl("http://gwxshop.sinaapp.com/assessment/toComprehensive");
		subList1.add(wxMenuTop0104);
		wxMenuTop01.setSubMenu(subList1);
		
		WxMenu wxMenuTop02 = new WxMenu();
		wxMenuTop02.setId(1L);
		wxMenuTop02.setType("click");
		wxMenuTop02.setName("学习园地");
		wxMenuTop02.setKey("MENU_LEVEL_02");
		
		WxMenu wxMenuTop0201 = new WxMenu();
		wxMenuTop0201.setId(2L);
		wxMenuTop0201.setType("view");
		wxMenuTop0201.setName("交流互通");
		wxMenuTop0201.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList2.add(wxMenuTop0201);
		
		WxMenu wxMenuTop0202 = new WxMenu();
		wxMenuTop0202.setId(2L);
		wxMenuTop0202.setType("view");
		wxMenuTop0202.setName("书籍推荐");
		wxMenuTop0202.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList2.add(wxMenuTop0202);
		
		WxMenu wxMenuTop0203 = new WxMenu();
		wxMenuTop0203.setId(2L);
		wxMenuTop0203.setType("view");
		wxMenuTop0203.setName("相关培训");
		wxMenuTop0203.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList2.add(wxMenuTop0203);
		
		WxMenu wxMenuTop0204 = new WxMenu();
		wxMenuTop0204.setId(2L);
		wxMenuTop0204.setType("view");
		wxMenuTop0204.setName("学习政策");
		wxMenuTop0204.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList2.add(wxMenuTop0204);
		
		wxMenuTop02.setSubMenu(subList2);
		
		WxMenu wxMenuTop03 = new WxMenu();
		wxMenuTop03.setId(7L);
		wxMenuTop03.setType("click");
		wxMenuTop03.setName("个人中心");
		wxMenuTop03.setKey("MENU_LEVEL_03");
		
		WxMenu wxMenuTop0301 = new WxMenu();
		wxMenuTop0301.setId(2L);
		wxMenuTop0301.setType("view");
		wxMenuTop0301.setName("充值记录");
		wxMenuTop0301.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList3.add(wxMenuTop0301);
		
		WxMenu wxMenuTop0302 = new WxMenu();
		wxMenuTop0302.setId(2L);
		wxMenuTop0302.setType("view");
		wxMenuTop0302.setName("账号绑定");
		wxMenuTop0302.setUrl("http://gwxshop.sinaapp.com/assessment/toScoreAnalysis");
		subList3.add(wxMenuTop0302);
		
		wxMenuTop03.setSubMenu(subList3);
		
		list.add(wxMenuTop01);
		list.add(wxMenuTop02);
		list.add(wxMenuTop03);
		
		Map<String, List<WxMenu>> map = new HashMap<String, List<WxMenu>>();
		map.put("button", list);
		String jsonWxMenu = JsonBinder.buildNonNullBinder().toJson(map);
		System.out.println(jsonWxMenu);
		
		ClientTest client = new ClientTest();
		String token = client.getAccessToken();
		System.out.println(token);
		
		client.createMenu(map,token);
	}
	
	public void createMenu(Object obj,String token){
		try {
			String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+token;
			String responseStr = HttpClientUtil.getInstance().postJson(url, obj);
			System.out.println(responseStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public String getAccessToken(){
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc552008ab3b75b65&secret=ddc02a8a53aa66ccd32407ae3629efd4";
		String token = "";
		try {
			String responseStr = HttpClientUtil.getInstance().get(url);
			Map<String,String> map = JsonBinder.buildNormalBinder().fromJson(responseStr, HashMap.class);
			token = map.get("access_token");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return token;
	}
}
